<?php
namespace sms;


use sms\yunsdk\Rest;
use think\facade\Log;


class YunSms implements SmsBase {
    public static function sendSms($photo,$code)
    {
        //主帐号
        $accountSid = config('sms.yunsms.accountSid');
        //主帐号Token
        $accountToken = config('sms.yunsms.accountToken');
        //应用Id
        $appId = config('sms.yunsms.appId');
        //请求地址，格式如下，不需要写https://
        $serverIP = 'app.cloopen.com';
        //请求端口
        $serverPort = '8883';
        //REST版本号
        $softVersion = '2013-12-26';
        // 初始化REST SDK
        $rest = new Rest($serverIP, $serverPort, $softVersion);
        $rest->setAccount($accountSid, $accountToken);
        $rest->setAppId($appId);

        // 发送模板短信
        $result = $rest->sendTemplateSMS($photo, [$code], config('sms.yunsms.tempId'));


        if ($result == NULL) {
//            echo "result error!";
            Log::error('yunsms-sendCode-exception'.'result error');
            return false;
        }
        if ($result->statusCode != 0) {
//            echo "error code :" . $result->statusCode . "<br>";
//            echo "error msg :" . $result->statusMsg . "<br>";
            //TODO 添加错误处理逻辑
            Log::error('yunsms-sendCode-exception'.$result->statusMsg);
            return false;
        } else {
//            echo "Sendind TemplateSMS success!<br/>";
            // 获取返回信息
            $smsmessage = $result->TemplateSMS;
//            echo "dateCreated:" . $smsmessage->dateCreated . "<br/>";
//            echo "smsMessageSid:" . $smsmessage->smsMessageSid . "<br/>";
            //TODO 添加成功处理逻辑
            Log::info("yunsms-sendCode-result:{$smsmessage->dateCreated}-{$photo}-{$smsmessage->smsMessageSid}");
            return true;
        }

    }
}